<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .menu{
            display: flex;
            height: 40px;
        }
        .menu .item {
            flex: 1;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: burlywood;
        }
        .menu .active{
            color: red;;
        }
    </style>

</head>

<body>
    <div id="app">
        <div class="menu">
            <div class="item" v-for="item in list" :class="{active:item.id==activeIndex}" @click="change(item)">
                {{item.msg}}
            </div>
        </div>
        <div class="box">
            <component :is="com"></component>
        </div>
       
    </div>
</body>
<template id="com1">
    <div>
        我是父组件 我自己的数据为{{num}}
        <child1 :n1="num"></child1>
    </div>
</template>
<template id="com2">
    <div>
        我是父组件
        显示子组件的数据为{{num}}
        <child2 @setnum="setNum"></child2>
        <!-- =前面只能是小写 -->
    </div>
</template>
<template id="com3">
    <div>
        我是组件三  <button @click="getNum">点击获取子组件的数据</button>
        <child3 ref="child3"></child3>
        <!-- ref就是类似原生里面的id -->
    </div>
</template>
<template id="com4">
    <div>
        我是做子传子的
    </div>
</template>
<template id="child1">
    <div>
        <h3>我是来自父组件的数据{{n1}}</h3>
    </div>
</template>
<template id="child2">
    <div>
        我是子组件
        数字  <input type="text" v-model="mynum">
    </div>
</template>
<template id="child3">
    <div>
        我是子组件
        我的数据为<input v-model="num" ref="num"/>
    </div>
</template>

<script>
    let  child1={//第一个子组件
        props:["n1"],
        template:"#child1"
    }

    let  child2={
        data(){
            return {
                mynum:100
            }
        },
        watch:{
            mynum(num){
                this.$emit("setnum",num);//调用自定义事件  setnum是函数名称   num是参数
            }
        },
        template:"#child2"
    }
    let  child3 = {
        template:"#child3",
        data(){
            return {
               num:"" 
            }
        }
    }

    let com1 = {
        data(){
            return {
                num:100
            }
        },
        template: "#com1",
        components:{
            child1
        }

    };
    let  com2={
        template:"#com2",
        data(){
            return  {
                num:"",//这个坑 是来装 子组件的数据
            }
        },
        methods:{
            setNum(num){ //准备一个方法 来填坑
                this.num = num;
            }
        },
        components:{
            child2
        }
    }
    let  com3={
        template:"#com3",
        components:{
            child3
        },
        methods:{
            getNum(){
                console.log(this.$refs.child3.num);
                console.log(this.$refs.child3.$refs.num.value);//document.getElementById("child3").getElementById("num").value;
                //refs 就等价于 id
            }
        }
    }
    let com4 = {
        template:"#com4"
    }
    new Vue({
        el: "#app",
        data: {
            com:"com1",
            activeIndex:1,
            list:[{id:1,msg:"父传子",com:"com1"},{id:2,msg:"子传父",com:"com2"},{id:3,msg:"子传父",com:"com3"},{id:4,msg:"子传子",com:"com4"}]
        },
        components: {
           com1,
           com2,
           com3,
           com4,
        },
        methods:{
            change(item){
                this.activeIndex = item.id;
                this.com = item.com;
            }
        }
    })

</script>

</html>